<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $attrList app\models\CardAttr[]
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '属性列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>

<div class="form-group">
    <a class="btn btn-success btn-sm" href="<?php echo Url::to(['/user/card/attr-edit']);?>">添加</a>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>名称</th>
        <th>在列表中显示</th>
        <th>类型</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($attrList as $attr) {?>
        <tr id="data_<?php echo $attr->id;?>">
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $attr->id;?>" /><span class="lbl"><?php echo $attr->id;?></span></label></td>
            <td><?php echo Html::encode($attr->name);?></td>
            <td><?php echo KeyMap::getValue('yes_no', $attr->show_in_list);?></td>
            <td><?php echo KeyMap::getValue('card_attr_type', $attr->type);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-pencil', 'href' => Url::to(['/user/card/attr-edit', 'id' => $attr->id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '修改', 'color' => 'green'],
                    ['icon' => 'fa fa-trash', 'onclick' => 'deleteCardAttr(' . $attr->id . ')', 'btn_class' => 'btn btn-xs btn-danger', 'tip' => '删除', 'color' => 'red'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<script>
    /**
     * 删除卡属性
     * @param id
     * @returns {boolean}
     */
    function deleteCardAttr(id) {
        if (!confirm('确定要删除此属性吗？')) {
            return false;
        }
        $.getJSON('/user/card/attr-delete', {id: id}, function (json) {
            if (callback(json)) {
                $('#data_' + id).remove();
            }
        });
    }
</script>
